package problems.contest;

import java.util.PriorityQueue;

/**
 * 第 327 场周赛
 * <p>https://leetcode.cn/contest/weekly-contest-327</p>
 *
 * @author habitplus
 * @since 8:54, 2023/1/8
 */
public class WCT327 {
    /**
     * T1|2529. 正整数和负整数的最大计数
     * <p>https://leetcode.cn/problems/maximum-count-of-positive-integer-and-negative-integer/</p>
     */
    public int maximumCount(int[] nums) {
        int act = 0;
        int neg = 0;

        for (int num : nums) {
            if (num > 0) ++act;
            else if (num < 0) ++neg;
        }

        return Math.max(act, neg);
    }



    /**
     * T2|6285. 执行 K 次操作后的最大分数
     * <p>https://leetcode.cn/problems/maximal-score-after-applying-k-operations/</p>
     * <li>贪心 + 大顶堆</li>
     */
    public long maxKelements(int[] nums, int k) {
        PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> o2.compareTo(o1));

        for (int num : nums) pq.offer(num);

        long ans = 0;
        int max;
        while (k-- > 0) {
            max = pq.poll();
            ans += max;
            pq.offer((int) Math.ceil(max * 1.0 / 3));
        }

        return ans;
    }



    /**
     * T3|6284. 使字符串总不同字符的数目相等
     * <p>https://leetcode.cn/problems/make-number-of-distinct-characters-equal/</p>
     * <li>hash计数 + 枚举</li>
     */
    public boolean isItPossible(String word1, String word2) {
        int sz = 26;
        int[] m1 = new int[sz];
        int[] m2 = new int[sz];

        for (char c : word1.toCharArray()) ++m1[c - 'a'];

        for (char c : word2.toCharArray()) ++m2[c - 'a'];

        for (int i = 0; i < sz; ++i) {
            if (m1[i] > 0) {
                // 交换 m1[i] 到 m2
                --m1[i];
                ++m2[i];
                for (int j = 0; j < sz; ++j) {
                    // 交换 m2[j]
                    if ((j != i && m2[j] > 0) || (j == i && m2[j] > 1)) {
                        --m2[j];
                        ++m1[j];
                        if (check(m1, m2)) return true;
                        ++m2[j];
                        --m1[j];
                    }
                }
                ++m1[i];
                --m2[i];
            }

        }

        return false;

    }

    private boolean check(int[] m1, int[] m2) {
        int cnt1 = 0;
        int cnt2 = 0;

        for (int i = 0; i < m1.length; ++i) {
            cnt1 += m1[i] > 0 ? 1 : 0;
            cnt2 += m2[i] > 0 ? 1 : 0;
        }
        return cnt1 == cnt2;
    }


}
